package com.eco.fanliapp.admin.controller.web;

import com.eco.fanliapp.admin.common.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.AppUserRedRewardMapper;
import com.eco.fanliapp.entity.AppUserRedReward;
import com.eco.fanliapp.service.UpdateUserCommon;
import com.eco.fanliapp.utils.EasyPoiStyleUtils;
import com.eco.fanliapp.utils.EasyPoiUtils;
import com.eco.fanliapp.vo.RedFristBuyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @date 2020-01-03
 */
@RestController
@RequestMapping("/web/red")
public class RedController extends BaseController {

    @Autowired
    private AppUserRedRewardMapper appUserRedRewardMapper;
    @Autowired
    private UpdateUserCommon updateUserCommon;

    /**
     * 首购红包处理
     * @return
     */
    @RequestMapping(value = "/fristBuy/dispose")
    @ResponseBody
    public ServerResponse fristBuyDownload(HttpServletResponse response){
        //1查询符合条件的用户及订单
        List<RedFristBuyVo> list =  appUserRedRewardMapper.selectFristBuyDownload();
        List<RedFristBuyVo> listTwo = new ArrayList<>();
        if(list == null || list.size() == 0){
            return ServerResponse.createByErrorMessage("没有满足条件的用户");
        }
        //处理订单
        for (RedFristBuyVo redVo:list
             ) {
            //根据用户id查询红包
          List<AppUserRedReward> redList = appUserRedRewardMapper.selectAppUserRedRewardList(redVo.getUserId());
            if(redList != null  && redList.get(1) != null && redList.get(1).getHbType() == 3){
                AppUserRedReward appUserRedReward = new AppUserRedReward();
                appUserRedReward.setTableId(redList.get(1).getTableId());
                appUserRedReward.setOrderId(redVo.getOrderId());
                appUserRedReward.setExpireTime(redVo.getBindingTime().getTime()+691200000);//收货时间 +8天
                //判断红包状态绑定订单，修改红包
                if(redVo.getTkStatus() ==3){
                    appUserRedReward.setHbStatus(2);
                    updateUserCommon.UpdateUserTaskStatus(redVo.getUserId(), 4);
                }else if(redVo.getTkStatus() ==12){
                    appUserRedReward.setHbStatus(7);
                    updateUserCommon.UpdateUserTaskStatus(redVo.getUserId(), 3);
                }
               int i = appUserRedRewardMapper.updateById(appUserRedReward);
                if (i >0){
                    redVo.setDescribe("成功");
                    listTwo.add(redVo);
                }else {
                    redVo.setDescribe("失败");
                    listTwo.add(redVo);
                }
            }else {
                redVo.setDescribe("没有可用红包");
                listTwo.add(redVo);
            }


            //添加日志 处理成功添加到listTwo
        }
        try{
            EasyPoiUtils.exportExcel(listTwo, "首购红包", "首购红包", RedFristBuyVo.class, EasyPoiStyleUtils.class, "RedFristBuy.xls", response);
        }catch (Exception e){
            return ServerResponse.createByErrorMessage("下载失败");
        }
        return ServerResponse.createBySuccessMessage("下载成功");
    }
}
